#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;

int main()
{
    pid_t id = fork();
    if (id == 0)
    {
        // child
        int cnt = 500;
        while (cnt)
        {
            cout << "I'm a child.My pid is " << getpid() << ".cnt:" << cnt << endl;
            sleep(1);
            --cnt;
        }
        exit(1);
    }
    else if (id > 0)
    {
        // father
        int status = 0;
        pid_t ret = waitpid(id, &status, 0);
        if (ret == id)
        {
            cout << "Child's pid:" << id << "  Exit code:" << ((status >> 8) & 0XFF) << 
            "  Exit signal:" << (status & 0x7f) << "  Core dump:" << ((status >> 7) & 1) << endl;
        }else{
            perror("waitpid");
        }
    }else{
        perror("fork");
    }
    return 0;
}